/*
Java source file
Copyright: Umschalt.com 2012
*/
package com.david.documentation.web.view.panels;

import com.david.documentation.domain.entities.doku.Doku;
import com.david.documentation.service.doku.DokuService;
import com.david.documentation.web.view.DokuUtils;
import com.david.documentation.web.view.components.DokuPagingNavigator;
import com.david.documentation.web.view.pages.DetailPage;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.spring.injection.annot.SpringBean;

public class SearchResultPanel extends Panel{

	private String suche;

	@SpringBean
	private DokuService dokuService;

	public SearchResultPanel(String id,SortableDataProvider dataProvider, String suche) {
		super(id);
		this.suche= suche;
		setOutputMarkupId(true);

		if (dataProvider.size() == 1){
			setResponsePage(DetailPage.class,new PageParameters("doku=" + ((Doku)dataProvider.iterator(0,1).next()).getId()));
		}

		add(new Label("count",dataProvider.size()+""));
		add(new Label("searchterm",suche));

		DataView<Doku> view = new DataView<Doku>("view", dataProvider) {
			
			@Override
			protected void populateItem(Item<Doku> dokuListItem) {
				Doku doku = dokuListItem.getModelObject();

				BookmarkablePageLink link = new BookmarkablePageLink("link", DetailPage.class,new PageParameters("doku="+doku.getId()+",search="+SearchResultPanel.this.suche));
				link.add(new Label("name", DokuUtils.markSearchString(SearchResultPanel.this.suche, doku.getName())).setEscapeModelStrings(false));
				link.add(new Label("description", DokuUtils.markSearchString(SearchResultPanel.this.suche,StringUtils.abbreviate(doku.getDescription(), 200))).setEscapeModelStrings(false));
				link.add(new WebMarkupContainer("image").add(new SimpleAttributeModifier("src", doku.getImage_url())));
				dokuListItem.add(link);
			}
		};
		view.setItemsPerPage(7);
		view.setOutputMarkupId(true);
		add(view);
		add(new DokuPagingNavigator("navigator", view).setVisible(view.getPageCount()>1));
	}

}
